package xyz.zichengdulin.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import xyz.zichengdulin.mapper.ClazzMapper;
import xyz.zichengdulin.pojo.Clazz;
import xyz.zichengdulin.pojo.ClazzQueryParam;
import xyz.zichengdulin.pojo.PageResult;
import xyz.zichengdulin.service.ClazzService;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl implements ClazzService {

    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 设置分页查询
     *
     * @param clazzQueryParam
     * @return
     */
    @Override
    public PageResult<Clazz> page(ClazzQueryParam clazzQueryParam) {
        //1.设置分页参数
        PageHelper.startPage(clazzQueryParam.getPage(), clazzQueryParam.getPageSize());

        //2.执行查询
        List<Clazz> clazzList = clazzMapper.list(clazzQueryParam);

        //3.根据时间戳设置课程状态--for循环便利表单
        for (Clazz clazz : clazzList) {
            if (clazz.getBeginDate().isAfter(LocalDate.now())) {
                clazz.setStatus("未开班");
            }
            if (clazz.getBeginDate().isBefore(LocalDate.now()) && clazz.getEndDate().isAfter(LocalDate.now())) {
                clazz.setStatus("在读中");
            }
            if (clazz.getEndDate().isBefore(LocalDate.now())) {
                clazz.setStatus("已结课");
            }
        }

        //4.解析查询结果，并封装
        Page<Clazz> p = (Page<Clazz>) clazzList;
        return new PageResult<Clazz>(p.getTotal(), p.getResult());
    }

    /**
     * 删除班级信息
     *
     * @param id
     */
    @Override
    public void delete(Integer id) {
        if (clazzMapper.getNumById(id) == 0) {
            clazzMapper.delete(id);
        } else {
            RuntimeException runtimeException = new RuntimeException();
            throw runtimeException;
        }

    }

    /**
     * 添加班级
     *
     * @param clazz
     */
    @Override
    public void insert(Clazz clazz) {
        //根据时间戳设置课程状态
        if (clazz.getBeginDate().isAfter(LocalDate.now())) {
            clazz.setStatus("未开班");
        }
        if (clazz.getBeginDate().isBefore(LocalDate.now()) && clazz.getEndDate().isAfter(LocalDate.now())) {
            clazz.setStatus("在读中");
        }
        if (clazz.getEndDate().isBefore(LocalDate.now())) {
            clazz.setStatus("已结课");
        }

        //当CreatTime值为空值，表示其为新创建的班级，自动为其创立时间和更新时间赋值
        if (clazz.getCreateTime() == null) {
            clazz.setCreateTime(LocalDateTime.now());
            clazz.setUpdateTime(LocalDateTime.now());
        }
        clazzMapper.insert(clazz);
    }

    /**
     * 查询所有班级信息
     */
    @Override
    public List<Clazz> getInfo() {
        List<Clazz> clazzlist = clazzMapper.getInfo();
        return clazzlist;
    }

    /**
     * 根据id查询班级信息
     *
     * @param id
     * @return
     */
    @Override
    public Clazz getInfoById(Integer id) {
        return clazzMapper.getInfoById(id);
    }

    /**
     * 修改班级
     *
     * @param clazz
     */
    @Override
    public void update(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazzMapper.update(clazz);
    }
}
